<?php
namespace app\api\controller;
use think\facade\Db;
class role extends common
{
    private $dbname = 'role';


    /**
     * 列表分页获取
     * @return string
     */
    public function getlist()
    {

        $data = input('get.');


        if($data['limit'] == 'all'){

            $list = Db::view($this->dbname,'id,name,status')
                ->order('id', 'desc')
                ->select()->toArray();
            return re_success('成功',$list);
        }else{

            $list = Db::view($this->dbname,'*')
                ->where('role.name|role.content','like','%'.$data['name'].'%')
                ->order('id', 'desc')
                ->paginate([
                    'list_rows'=> $data['limit'],
                    'var_page' => 'page',
                ])->toArray();
            return re_list($list);

        }
    }

    /**
     * 添加
     * @return false|string|void
     */
    public function insert()
    {
        $data = input('post.');
        //模型数据验证
        $model = new \app\api\model\Role();
        //模型写入
        $data['menu_ids'] =  $data['menu_ids']?implode(',',$data['menu_ids']):null;
        $data['time']= ThisTime();
        return $model->add($data);
    }

    /**
     * 修改
     * @return false|string|void
     */
    public function update()
    {
        $data = input('put.');
        //模型验证
//        dump($data['menu_ids']);
//        exit();
        $data['menu_ids'] = $data['menu_ids']?implode(',',$data['menu_ids']):null;

        $model = new \app\api\model\Role();
        return $model->edit($data);

    }

    /**查询采用视图查询
     * @return false|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function info()
    {
        $info = Db::name($this->dbname)
            ->find(input('get.id'));
        if($info){
            $info['menu_ids'] = $info['menu_ids']?explode(',',$info['menu_ids']):[];
            return re_success('获取成功',$info);
        }else{
            return re_error('获取失败');
        }
    }


    /**
     * 删除
     * @return false|string|void
     */
    public function delete()
    {
        $data = input('delete.id');
        $model = new \app\api\model\Role();
        return $model->dele($data);
    }
}
